package formework.aop.support;

import formework.aop.config.DemoAopConfig;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DemoAdvisedSupport {

    Class<?>  targetClass;

    Object target;

    private transient Map<Method, List<Object>> methodCache;

    private DemoAopConfig config;

    private Pattern pointCutClassPattern;

    public DemoAdvisedSupport(DemoAopConfig config){
        this.config=config;
    }

    public void setTargetClass(Class clazz) {
        this.targetClass = clazz;
        parse();
    }
    public List<Object> getInterceptorsAndDynamicInterceptionAdvice(Method method, Class<?> targetClass) throws Exception{
        List<Object> cached = methodCache.get(method);
        if(cached == null){
            Method m = targetClass.getMethod(method.getName(), method.getParameterTypes());
            cached = methodCache.get(m);
            methodCache.put(m,cached);
        }
        return cached;
    }

    private void parse() {
        String pointCut = config.getPointCut()
                .replaceAll("\\.","\\\\.")
                .replaceAll("\\\\.\\*",".*")
                .replaceAll("\\(","\\\\(")
                .replaceAll("\\)","\\\\)");
        //pointCut=public .* com.gupaoedu.vip.spring.demo.service..*Service..*(.*)
        //玩正则
        String pointCutForClassRegex = pointCut.substring(0,pointCut.lastIndexOf("\\(") - 4);
        pointCutClassPattern = Pattern.compile("class " + pointCutForClassRegex.substring(
                pointCutForClassRegex.lastIndexOf(" ") + 1));

        try {
            methodCache = new HashMap<Method, List<Object>>();
            Pattern pattern = Pattern.compile(pointCut);
            Class<?> aspectClass = Class.forName(config.getAspectClass());

            Method[] methods = aspectClass.getMethods();
            for(Method method : methods){

                String m = method.getName();
            }

            for (Method m : this.targetClass.getMethods()){
                String methodString = m.toString();

                if(methodString.contains("throws")){
                    methodString = methodString.substring(0, methodString.lastIndexOf("throws")).trim();
                }
                Matcher matcher = pattern.matcher(methodString);
                if(matcher.matches()){
                    List<Object> advices = new ArrayList<Object>();

                    if(!(null == config.getAspectBefore() || "".equals(config.getAspectBefore()))) {
                        advices.add(new Demo)
                    }
                }

            }


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


        Matcher matcher = pointCutClassPattern.matcher(targetClass.getName());

    }

    public void setTarget(Object o) {
        target = o;
    }

    public boolean pointCutMatch() {
        return pointCutClassPattern.matcher(this.targetClass.toString()).matches();

    }

    public Class<?> getTargetClass() {
        return targetClass;
    }

    public Object getTarget() {
        return target;
    }
}
